package com.drinks.yiniang.ui.widget.dialog;

import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.view.inputmethod.InputMethodManager;
import android.widget.EditText;
import android.widget.TextView;

import com.drinks.yiniang.R;
import com.hansen.library.Constants;
import com.hansen.library.ui.widget.dialog.BaseDialogFragment;
import com.hansen.library.utils.LogUtils;

/**
 * @author han
 * @date 2020/1/9
 */
public class CommentBoxDialog extends BaseDialogFragment implements View.OnTouchListener {

    private Dialog mDialog;

    //输入评论
    private EditText et_news_comment;
    //发表
    private TextView tv_comment_publish;

    private OnSendCommentListener onSendCommentListener;

    private int position = -1;

    public CommentBoxDialog() {
    }

    public static CommentBoxDialog newInstance(int position) {
        Bundle bundle = new Bundle();
        bundle.putInt(Constants.KeyPos, position);

        CommentBoxDialog dialog = new CommentBoxDialog();
        dialog.setArguments(bundle);

        return dialog;
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        try {
            if(getParentFragment() != null) {
                onSendCommentListener = (OnSendCommentListener) getParentFragment();
            } else {
                onSendCommentListener = (OnSendCommentListener) context;
            }
        } catch (ClassCastException e) {
            LogUtils.e(context.toString() + "must implement OnSendCommentListener");
        }
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // 定义Dialog布局和参数
        mDialog = new Dialog(getActivity(), R.style.CommentDialogStyle);

        // 获取Dialog布局
        View view = LayoutInflater.from(getActivity()).inflate(
                R.layout.dialog_comment_box, null);

        mDialog.requestWindowFeature(Window.FEATURE_NO_TITLE); // 设置Content前设定
        mDialog.setContentView(view);
        mDialog.setCanceledOnTouchOutside(true);
        mDialog.setCancelable(true);

        et_news_comment = view.findViewById(R.id.et_news_comment);
        tv_comment_publish = view.findViewById(R.id.tv_comment_publish);

        Window dialogWindow = mDialog.getWindow();
        dialogWindow.setGravity(Gravity.LEFT | Gravity.BOTTOM);
        WindowManager.LayoutParams lp = dialogWindow.getAttributes();

        lp.gravity = Gravity.BOTTOM; // 紧贴底部
        lp.dimAmount = 0.5f;
        lp.width = WindowManager.LayoutParams.MATCH_PARENT; // 宽度持平

        dialogWindow.setAttributes(lp);
        dialogWindow.addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);

        mDialog.getWindow().getDecorView().setOnTouchListener(this);

        et_news_comment.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                if (s.length() > 0) {
                    tv_comment_publish.setEnabled(true);
                } else {
                    tv_comment_publish.setEnabled(false);
                }

            }
        });

        tv_comment_publish.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (onSendCommentListener != null) {
                    onSendCommentListener.sendComment(et_news_comment.getText().toString(), position);
                }
            }
        });

        position = getArguments() != null ? getArguments().getInt(Constants.KeyPos, -1) : -1;

        return mDialog;
    }

    @Override
    public void dismiss() {
        View view = getDialog().getCurrentFocus();
        if(view != null){
            hideKeyboard(view.getWindowToken());
        }
        super.dismiss();
    }

    @Override
    public boolean onTouch(View view, MotionEvent event) {
        if(isShouldHideKeyBoard(et_news_comment, event)) {
            if(getDialog().getCurrentFocus() != null
                    && getDialog().getCurrentFocus().getWindowToken() != null) {
                hideKeyboard(getDialog().getCurrentFocus().getWindowToken());
            }

        }
        return false;
    }

    public interface OnSendCommentListener {
        void sendComment(String comment, int position);
    }

    @Override
    public void onDestroy() {
        mDialog = null;
        onSendCommentListener = null;
        super.onDestroy();
    }
}